package com.pan.admin.module.visualizer.controller;

import com.gitee.apanlh.util.base.Empty;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.base.MapUtils;
import com.gitee.apanlh.util.date.DateUtils;
import com.gitee.apanlh.util.date.format.DateTimeFormat;
import com.gitee.apanlh.util.valid.ValidParam;
import com.pan.admin.common.core.controller.BaseController;
import com.pan.admin.common.core.page.TableDataInfo;
import com.pan.admin.module.visualizer.entity.po.LoanProductVisualizerPO;
import com.pan.admin.module.visualizer.entity.qo.LoanProductVisualizerQO;
import com.pan.admin.module.visualizer.entity.vo.LoanProductVisualizerVO;
import com.pan.admin.module.visualizer.entity.vo.ReqLoanProductVisualizerVO;
import com.pan.admin.module.visualizer.service.LoanProductVisualizerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Comparator;
import java.util.List;
import java.util.Map;

/**
 *  主体业务数据统计
 *  @author 周硕
 */
@RestController
@RequestMapping("/visualizer/loanProduct")
public class LoanProductVisualizerController extends BaseController {

    @Autowired
    private LoanProductVisualizerService loanProductVisualizerService;

    /**
     *  获取主体业务数据统计数据列表
     *
     *  @return
     */
    @PreAuthorize("@ss.hasPermi('visualizer:loanProduct:query')")
    @GetMapping("/list")
    public TableDataInfo list(ReqLoanProductVisualizerVO reqLoanProductVisualizerVO) {
        Map<String, Object> params = reqLoanProductVisualizerVO.getParams();
        String startTime = String.valueOf(params.get("beginTime"));
        String endTime = String.valueOf(params.get("endTime"));
        boolean hasNow = false;
        if (ValidParam.isEmpty(startTime) && ValidParam.isEmpty(endTime)) {
            String date = DateUtils.currentTime(DateTimeFormat.DATE_FULL);
            startTime = date + " 00:00:00";
            endTime = DateUtils.currentTime();
            hasNow = true;
        } else {
            startTime = startTime + " 00:00:00";
            endTime = endTime + " 00:00:00";
        }

        String displayDateTime;
        //  拼接时间
        if (hasNow) {
            displayDateTime = "当日数据截止至" + endTime;
        } else {
            displayDateTime = startTime + "至" + endTime;
        }

        //  获取有撞库进件信息的loanProduct 的id,name,status
        //  status对应关系：ProductApplyFlowStatusEnum
        LoanProductVisualizerQO loan = new LoanProductVisualizerQO();
        loan.setLoanProductName(reqLoanProductVisualizerVO.getLoanProductName());
        loan.setStartTime(startTime);
        loan.setEndTime(endTime);
        List<LoanProductVisualizerPO> list = loanProductVisualizerService.listLoanProductProduct(loan);
        if (ValidParam.isEmpty(list)) {
            return getDataTable(Empty.list());
        }
        //  转换VO实体
        List<LoanProductVisualizerVO> convert = LoanProductVisualizerVO.convert(list);

        convert.forEach(apiVisualizerVO -> apiVisualizerVO.setDateTime(displayDateTime));
        //  排序
        Map<String, Comparator<LoanProductVisualizerVO>> comparatorMap = MapUtils.newLinkedHashMap(map -> {
            map.put("loanProductDataExchangeSucTotal", Comparator.nullsFirst(Comparator.comparing(LoanProductVisualizerVO::getLoanProductDataExchangeSucTotal)));
            map.put("loanProductDataExchangeFailTotal", Comparator.nullsFirst(Comparator.comparing(LoanProductVisualizerVO::getLoanProductDataExchangeFailTotal)));
            map.put("loanProductApplyBizSucTotal", Comparator.nullsFirst(Comparator.comparing(LoanProductVisualizerVO::getLoanProductApplyBizSucTotal)));
            map.put("loanProductApplyBizFailTotal", Comparator.nullsFirst(Comparator.comparing(LoanProductVisualizerVO::getLoanProductApplyBizFailTotal)));
        });

        Comparator<LoanProductVisualizerVO> comparator = comparatorMap.get(reqLoanProductVisualizerVO.getSortField());
        if (comparator == null) {
            comparator = comparatorMap.get("loanProductApplyBizSucTotal");
        }
        if (!Eq.str(reqLoanProductVisualizerVO.getSort(), "ascending")) {
            comparator = comparator.reversed();
        }

        Comparator<LoanProductVisualizerVO> comparator2 = comparator;
        // 将数据进行排序
        convert.sort(comparator2);
        return getDataTable(convert);
    }
}
